package com.urick.notify.nacos;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.urick.notify.nacos.event.Event;
import com.urick.notify.nacos.manager.NotifyManager;
import com.urick.notify.nacos.sub.Subscriber;

import java.util.concurrent.TimeUnit;

/**
 * Design Theories:
 * <a href=""></a>
 *
 * @author uRick 2021/11/25 16:38
 * Email: rickwork@163.com
 * Wechat: yy3076815642
 * QQ: 3076815642
 * @since 1.0
 */
public class NacosCase {
    private static final Log log = LogFactory.get();

    public static void main(String[] args) {
        //1.同步监听
        NotifyManager.registerSubscriber(new Subscriber<RefreshEvent>() {
            @Override
            public void onEvent(RefreshEvent event) {
                log.info("收到订阅触发事件");
            }

            @Override
            public Class<? extends Event> subscribeType() {
                return RefreshEvent.class;
            }
        });

        //2.异步通知
        NotifyManager.registerSubscriber(new Subscriber<RefreshEvent>() {
                                             @Override
                                             public void onEvent(RefreshEvent event) {
                                                 log.info("收到异步通知订阅触发事件");
                                             }

                                             @Override
                                             public Class<? extends Event> subscribeType() {
                                                 return RefreshEvent.class;
                                             }

                                         }
        );
        NotifyManager.publishEvent(new RefreshEvent());
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    static class RefreshEvent extends Event {
    }
}
